package com.heima.comment.service.impl;

import com.heima.comment.dtos.CommentRepayDto;
import com.heima.comment.dtos.CommentRepayLikeDto;
import com.heima.comment.dtos.CommentRepaySaveDto;
import com.heima.comment.pojo.Comment;
import com.heima.comment.service.CommentRepayService;

import com.heima.model.common.dtos.ResponseResult;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class CommentRepayServiceImpl implements CommentRepayService {
    private final MongoTemplate mongoTemplate;

    /**
     * 评论回复保存
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveRepay(CommentRepaySaveDto dto) {
        Comment comment = new Comment();
        comment.setCommentId(dto.getCommentId());
        comment.setContent(dto.getContent());
        mongoTemplate.save(dto);
        return ResponseResult.okResult(null);
    }

    /**
     * 查询评论回复列表
     * @param dto
     * @return
     */
    @Override
    public List<Comment> repayList(CommentRepayDto dto) {
        int pageNum = 1;
        int pageSize = dto.getSize();
        PageRequest page = PageRequest.of(pageNum, pageSize, Sort.by(Sort.Direction.DESC, "createdTime"));
        Query query = Query.query(Criteria.where("entryId").is(dto.getCommentId())
                        .and("createdTime").lt(dto.getMinDate())
                )
                .with(Sort.by(Sort.Direction.DESC, "createdTime"))
                .with(page);
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        //封装返回结果
        return comments;
    }

    /**
     * 点赞喜欢回复
     * @param dto
     * @return
     */
    @Override
    public ResponseResult repayLike(CommentRepayLikeDto dto) {

        return null;
    }
}
